#include "stdafx.h"
#include "wf_drawlinefree.h"
#include "pf_snapper.h"
#include "pf_point.h"



WF_DrawLineFree::WF_DrawLineFree(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_ActionInterface("Draw freehand lines",
                    container, graphicView) {
    vertex = PF_Vector(false);
    polyline = NULL;
}

WF_DrawLineFree::~WF_DrawLineFree() {
    if (polyline!=NULL) {
        delete polyline;
    }
}

QAction* WF_DrawLineFree::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr(""Line: Freehand"),
    QAction* action = new QAction(tr("&Freehand Line"), NULL);
	action->setIcon(QIcon(":/extui/linesfree.png"));
    //action->zetStatusTip(tr("Draw freehand lines"));
    return action;
}

void WF_DrawLineFree::trigger() {
    if (polyline!=NULL) {
        container->addEntity(polyline);

        if (document) {
            document->startUndoCycle();
            document->addUndoable(polyline);
            document->endUndoCycle();
        }

		graphicView->redraw(LX_Define::RedrawDrawing);
        PF_DEBUG->print("PF_ActionDrawLineFree::trigger():"
                        " polyline added: %d", polyline->getId());
        polyline = NULL;
    }
}



void WF_DrawLineFree::mouseMoveEvent(PF_MouseEvent* e) {
    if (vertex.valid && polyline!=NULL) {
        PF_Vector v = snapPoint(e);
        PF_Entity* ent = polyline->addVertex(v);
        ent->setLayerToActive();
        ent->setPenToActive();

        graphicView->drawEntity(ent);
        drawSnapper();

        vertex = v;

        PF_DEBUG->print("PF_ActionDrawLineFree::mouseMoveEvent():"
                        " line added: %d", ent->getId());
    }
}



void WF_DrawLineFree::mousePressEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        vertex = snapPoint(e);
        polyline = new PF_Polyline(container,
                                   PF_PolylineAttribute(vertex, vertex, 0));
        polyline->setLayerToActive();
        polyline->setPenToActive();
    }
    //else if (LX_Define::qtToRsButtonState(e->button())==LX_Define::RightButton && !vertex.valid) {
    //}
}



void WF_DrawLineFree::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        vertex = PF_Vector(false);
        trigger();
    } else if (e->button()==Qt::RightButton) {
        if (polyline!=NULL) {
            delete polyline;
            polyline = NULL;
        }
        init(getStatus()-1);
    }
}



void WF_DrawLineFree::updateMouseButtonHints() {
    switch (getStatus()) {
    case 0:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Click and drag to draw a line"), tr("Cancel"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DrawLineFree::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawLineFree::updateToolBar() {
    if (!isFinished()) {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
    } else {
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
    }
}

// EOF
